package downbody

import (
	"bytes"
	"encoding/binary"
	"gitee.com/Cookie_XiaoD/go-terminalserver/protocol/std808core/body"
	"gitee.com/Cookie_XiaoD/go-terminalserver/protocol/std808core/msg"
)

//平台通用应答消息体
type GeneralRespBody struct {
	//应答流水号(对应的终端消息的流水号)
	SerialNumber uint16
	//应答ID(对应的终端消息的 ID)
	ID msg.Std808TerminalMsgID
	//结果
	Result body.PlatformGeneralRespResult
}

func NewGeneralRespBody(sn uint16, id msg.Std808TerminalMsgID, result body.PlatformGeneralRespResult) *GeneralRespBody {
	return &GeneralRespBody{
		SerialNumber: sn,
		ID:           id,
		Result:       result,
	}
}

func (body *GeneralRespBody) To808Bytes() []byte {
	var err error
	writer := new(bytes.Buffer)
	err = binary.Write(writer, binary.BigEndian, body.SerialNumber)
	err = binary.Write(writer, binary.BigEndian, body.ID)
	err = binary.Write(writer, binary.BigEndian, body.Result)
	if err != nil {
		return nil
	}
	return writer.Bytes()
}
